package kr.co.skplanet.commute.service;

import java.util.List;

import kr.co.skplanet.commute.dao.BoardDAO;
import kr.co.skplanet.commute.dao.CommentDAO;
import kr.co.skplanet.commute.domain.Board;
import kr.co.skplanet.commute.domain.Comment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service(value="boardService")
@Transactional
public class BoardService {
	@Autowired
	private BoardDAO boardDAO;
	@Autowired
	private CommentDAO commentDAO;
	
	@Transactional(readOnly = true)
	public Board getBoard(long id){
		Board board = boardDAO.findUniqByObject("id", id);
		return board;
	}
	
	@Transactional
	public void updateReadCount(long id){
		Board board = boardDAO.findUniqByObject("id", id);
		board.setReadcount(board.getReadcount() + 1);
	}
	
	@Transactional(readOnly = true)
	public int getBoardCount(){
		return boardDAO.getAll().size();
	}
	
	@Transactional(readOnly = true)
	public List<Board> getBoardList(int pg){
		return boardDAO.getBoardList(pg);
	}
	
	@Transactional
	public void addBoard(Board board){
		boardDAO.save(board);
	}
	
	@Transactional
	public void updateBoard(Board board){
		Board b1 = boardDAO.findUniqByObject("id", board.getId());
		b1.setName(board.getName());
		b1.setContent(board.getContent());
		b1.setTitle(board.getTitle());
	}
	

	@Transactional
	public void deleteBoard(Board board){
		boardDAO.delete(board);
	}	
	
	@Transactional
	public void addComment(long board_id, Comment comment){
		Board board = getBoard(board_id);
		comment.setBoard(board);
		board.addComment(comment);
	}
}
